﻿using QuanLyChoVayCaNhan.Classes;
using QuanLyChoVayCaNhan.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QuanLyChoVayCaNhan.Forms
{
    public partial class ViewTiepNhanHoSo : Form
    {
        private HoSo dataHoSo = null;
        private TiepNhanHS dataTiepNhan = null;
        private int SelectedIndex = -1;
        public ViewTiepNhanHoSo()
        {
            InitializeComponent();
            dataHoSo = (HoSo)WindowsManager.GetInstance().TabTiepNhanHoSo.SelectedItem;
        }

        private void ViewTiepNhanHoSo_Load(object sender, EventArgs e)
        {
            reloadTiepNhan();
            hoSoBindingSource.DataSource = dataHoSo;
        }
        // dong
        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        // tiep nhan bo xung
        private void button2_Click(object sender, EventArgs e)
        {
            if (!UserManager.GetInstance().IsCBQHKH())
            {
                MessageBox.Show("Bạn không có quyền tiếp nhận bổ sung hồ sơ");
                return;
            }
            if (dataHoSo.MaNVQHKH != UserManager.GetInstance().GetMaNhanVien())
            {
                MessageBox.Show("Bạn không có quyền tiếp bổ sung cho hồ sơ này");
                return;
            }
            AddTiepNhanBoSung fTiepNhanBoSung = new AddTiepNhanBoSung();
            fTiepNhanBoSung.FormClosed += fTiepNhanBoSung_FormClosed;
            fTiepNhanBoSung.ShowDialog();
        }

        void fTiepNhanBoSung_FormClosed(object sender, FormClosedEventArgs e)
        {
            reloadTiepNhan();
        }
        // in giay tiep nhan
        private void button3_Click(object sender, EventArgs e)
        {
            if (!UserManager.GetInstance().IsCBQHKH() && !UserManager.GetInstance().IsAdmin())
            {
                MessageBox.Show("Bạn không có quyền in giấy tiếp nhận hồ sơ");
                return;
            }
            if (SelectedIndex == -1)
            {
                MessageBox.Show("Bạn chưa chọn giấy tiếp nhận hồ sơ khách hàng nào");
                return;
            }
            TiepNhanHS tiepNhan = ModelManager.GetInstance().db.TiepNhanHS.ToList().FirstOrDefault((x) => x.So == dataTiepNhan.So && x.NgayTiepNhan.Date.CompareTo(dataTiepNhan.NgayTiepNhan.Date) == 0);
            PrintManager.GetInstance().InGiayTiepNhanHoSo(tiepNhan);
        }

        private void reloadTiepNhan()
        {
            tiepNhanHSsBindingSource.DataSource = ModelManager.GetInstance().db.TiepNhanHS.ToList().FindAll((x) => x.MaHS == dataHoSo.MaHS);
            if (tiepNhanHSsBindingSource.Count == 0 || tiepNhanHSDataGridView.CurrentRow == null)
            {
                SelectedIndex = -1;
                tiepNhanHSBindingSource.DataSource = dataTiepNhan = new TiepNhanHS();
            }
            else
            {
                SelectedIndex = tiepNhanHSDataGridView.CurrentRow.Index;
                tiepNhanHSBindingSource.DataSource = dataTiepNhan = (TiepNhanHS)tiepNhanHSsBindingSource.Current;
            }
        }

        private void tiepNhanHSDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void tiepNhanHSDataGridView_Click(object sender, EventArgs e)
        {
            if (tiepNhanHSsBindingSource.Count == 0 || tiepNhanHSDataGridView.CurrentRow == null)
            {
                SelectedIndex = -1;
                tiepNhanHSBindingSource.DataSource = dataTiepNhan = new TiepNhanHS();
            }
            else
            {
                SelectedIndex = tiepNhanHSDataGridView.CurrentRow.Index;
                tiepNhanHSBindingSource.DataSource = dataTiepNhan = (TiepNhanHS)tiepNhanHSsBindingSource.Current;
            }
        }
    }
}
